package com.fast.alden.admin.web.controller.system;

import com.fast.alden.admin.annotation.OperationLog;
import com.fast.alden.admin.entity.dto.ResetPasswordParam;
import com.fast.alden.admin.service.SysUserService;
import com.fast.alden.common.annotation.IgnoreRestControllerResponseAdvice;
import com.fast.alden.data.model.SysOperationType;
import com.fast.alden.data.model.SysUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 后台用户管理控制器
 */
@Tag(name = "SysUserController", description = "后台用户管理")
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Resource
    private SysUserService userService;

    @Operation(summary = "分页查询", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/page")
    public Page<SysUser> page(@RequestBody @Nullable SysUser searchVo, @RequestParam(name = "no", defaultValue = "0") int no, @RequestParam(name = "size", defaultValue = "10") int size) {
        return userService.search(searchVo, PageRequest.of(no, size, Sort.by(Sort.Direction.DESC, "createdTime")));
    }

    @Operation(summary = "根据ID查询", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @GetMapping("/retrieve/{id}")
    public SysUser retrieve(@PathVariable("id") Long id) {
        return userService.retrieve(id);
    }

    @OperationLog(title = "用户管理", type = SysOperationType.CREATE)
    @Operation(summary = "创建用户", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/create")
    public Long create(@RequestBody SysUser user) {
        return userService.create(user).getId();
    }

    @OperationLog(title = "用户管理", type = SysOperationType.UPDATE)
    @Operation(summary = "修改用户", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PutMapping("/update")
    public void update(@RequestBody SysUser user) {
        userService.update(user);
    }

    @OperationLog(title = "用户管理", type = SysOperationType.DELETE)
    @Operation(summary = "删除用户", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @DeleteMapping("/remove")
    public void remove(@RequestParam("ids") List<Long> ids) {
        userService.remove(ids);
    }

    @OperationLog(title = "用户管理", type = SysOperationType.UPDATE)
    @Operation(summary = "修改指定用户头像", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/update-avatar/{id}")
    public void updateAvatar(@PathVariable("id") Long id, @RequestParam("file") MultipartFile file) throws IOException {
        userService.updateAvatar(id, file);
    }

    @OperationLog(title = "用户管理", type = SysOperationType.UPDATE)
    @Operation(summary = "删除指定用户头像", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/remove-avatar/{id}")
    public void removeAvatar(@PathVariable("id") Long id) {
        userService.removeAvatar(id);
    }

    @OperationLog(title = "用户管理", type = SysOperationType.UPDATE)
    @Operation(summary = "重置指定用户密码", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/reset-password")
    public void resetPassword(@RequestBody @Validated ResetPasswordParam param) {
        userService.resetPassword(param.getId(), param.getPassword());
    }

    @OperationLog(title = "用户管理", type = SysOperationType.EXPORT)
    @Operation(summary = "导出全部用户数据", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/export")
    @IgnoreRestControllerResponseAdvice
    public void exportExcel() throws IOException {
        userService.exportExcel();
    }

    @OperationLog(title = "用户管理", type = SysOperationType.IMPORT)
    @Operation(summary = "导入用户数据", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/import")
    public void importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        userService.importExcel(file);
    }
}
